package gui.utils.components;


import java.awt.*;
import javax.swing.*;


/**
 * @author Charles Perin
 * 29 mars 2011
 * LIMSI - INRIA Rennes
 * charles.perin@free.fr
 *
 */
public	class ComboBoxWithIcons extends JLabel implements ListCellRenderer {


	/**
	 * 
	 */
	private static final long serialVersionUID = -4252863117811063843L;
	private ImageIcon[] images;

	public ComboBoxWithIcons(ImageIcon[] images) {
		this.images = images;
		setOpaque(true);
		setHorizontalAlignment(CENTER);
		setVerticalAlignment(CENTER);
	}

	/*
	 * This method finds the image and text corresponding
	 * to the selected value and returns the label, set up
	 * to display the text and image.
	 */
	public Component getListCellRendererComponent(
			JList list,
			Object value,
			int index,
			boolean isSelected,
			boolean cellHasFocus) {
		//Get the selected index. (The index param isn't
		//always valid, so just use the value.)
		int selectedIndex = ((Integer)value).intValue();

		if (isSelected) {
			setBackground(list.getSelectionBackground());
			setForeground(list.getSelectionForeground());
		} else {
			setBackground(list.getBackground());
			setForeground(list.getForeground());
		}

		//Set the icon and text.  If icon was null, say so.
		ImageIcon icon = images[selectedIndex];
		setIcon(icon);
		if (icon != null) {
			setFont(list.getFont());
		} else {
			System.err.println("no image found");
		}

		return this;
	}
}


